<?php
class GermanbashFetcher extends AbstractFetcher {
	
	/**
	 * @var string
	 */
	private $source;
	
	/**
	 * @see AbstractFetcher#setContentInformation()
	 */
	protected function setContentInformation(){
		if(preg_match('/http:\/\/.*german-bash.org\/(\d*)\/?/', $this->url, $matches) !== false && count($matches) == 2){
			$this->contentInformation = $matches[1];
			return $this->loadSource();
		}
		return false;
	}
	
	/**
	 * @see AbstractFetcher#setTitle()
	 */
	protected function setTitle(){
		$this->title = 'German-bash.org Zitat: '.$this->contentInformation;
	}
	
	/**
	 * @see AbstractFetcher#setDescription()
	 */
	protected function setDescription(){
		if(preg_match('/<div class="zitat">(.*)<\/div>/sU', $this->source, $matches) !== false && count($matches) == 2){
			$data = $matches[1];
			$search = array('<span class="quote_zeile">', '</span>');
			$replace = array('', "\n");
			$data = str_replace($search, $replace, $data);
			$data = preg_replace('/\s{2,}/', ' ', $data);
			$this->description = $data;
		}
	}
	
	/**
	 * @see AbstractFetcher#setPreviewImages()
	 */
	protected function setPreviewImages(){
		$this->previewImages[] = 'http://german-bash.org/images/hplogo2.jpg';
	}
	
	/**
	 * fetches source from url
	 * @return void
	 */
	private function loadSource(){
		require_once('CurlWrapper.php');
		$source = CurlWrapper::getSource($this->url);
		if($source){
			$this->source = $source;
			return true;
		}
		else{
			$this->source = false; //error occurred
			return false;
		}
	}	
}